import sys, os

from plugins.plugin import Plugin

sys.path.append("..")

from src.video import Video
from src.config import Config
from src.history import History

class Downloads(Plugin):

    def __init__(self, history: History=History(), config: Config=Config()) -> None:
        super().__init__(history, config)

        self.history = history
        self.config = config

        self.name = "[+] Downloads"
        self.category = "Downloads"

        self.query_description = """"""

        self.params = {
            "path" : False
        }

        self.flag = "-d"
        self.full_flag = "--downloads"
        self.flag_help = "show rosen downloads list"
        self.flag_action = "store_true"
        
        self.extensions = ('.mp3', '.mp4', '.mkv', '.webm', '.m3u')

    def get_downloads(self, path):
        items = []
        path = os.path.expanduser('~') + "/" + path
        for filename in os.listdir(path):
            if filename.endswith(self.extensions):
                video = Video(playlist_title=filename,
                                    url=path+"/"+filename)
                items.append(video.to_json())

        return items

    def get_items(self, params):
        path = "Downloads"
        if 'path' in params:
            path = params['path']
        return self.get_downloads(path)


